﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class select_course_detail : System.Web.UI.Page
{
    string Cno;
    string Cname;
    string category;
    string term;
    string credit;
    string address;
    string time;
    string Sno;

    protected void Page_Load(object sender, EventArgs e)
    {
        Cno = Request.QueryString["Cno"];
        Cname = Request.QueryString["Cname"];
        category = Request.QueryString["category"];
        term = Request.QueryString["term"];
        credit = Request.QueryString["credit"];
        address = Request.QueryString["address"];
        time = Request.QueryString["time"];

        Response.Write(" <center><h2>请确认选课：课程名：'" + Cname + "'，课程类别：'" + category + "' ，学分：'" + credit + "'</h2></center> ");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Sno = (string)Session["id"];
        int priority_no = Convert.ToInt32(Request.QueryString["priority_no"]);
        priority_no--;//选的这门课的前一门课的先修序号，判断这门课是否存在，存在便可选择当前要选的课，否则不让选。

        string conn = "Data Source= . ;Initial Catalog=XSXK; User ID=sa; pwd=123";
        SqlConnection connection = new SqlConnection(conn);

        try
        {
            connection.Open();
            SqlCommand comm = new SqlCommand();
            comm.Connection = connection;
            string str_sql;  //保存能成功插入数据库的情况下的SQL语句
            string str_sql1 = " select count(*) from result,Course where result.Sno='" + Sno + "' and result.Cno=Course.Cno and Course.category='" + category + "' and Course.priority_no='" + priority_no + "' ";  //判断是否有先修课程
            string str_sql2 = " select count(*) from Select_course_temp,Course where Select_course_temp.Sno='" + Sno + "' and Select_course_temp.Cno=Course.Cno and Course.time='" + time + "'  "; //查询上课时间是否有冲突
            string str_sql3 = " select sum(credit) from Course,Select_course_temp where Select_course_temp.Sno='" + Sno + "' and Select_course_temp.Cno=Course.Cno  "; //学生一学期的选课总学分不超过18
            string str_sql4 = " select flag_register from Student where Sno='" + Sno +"' ";//获取注册信息
            string str_sql5 = " select flag_quit from Student where Sno='" + Sno +"' ";//获取学生退学信息
            string str_sql6 = " select flag_rest from Student where Sno='" + Sno +"' ";//获取学生休学信息

            comm.CommandText = str_sql1;//执行第一句先修课程
            int count1 = Convert.ToInt32(comm.ExecuteScalar());

            comm.CommandText = str_sql2;//执行第二句，时间冲突
            int count2 = Convert.ToInt32(comm.ExecuteScalar());
            
            comm.CommandText = str_sql3;//执行第三句，选课总学分
            //int sum = Convert.ToInt32(comm.ExecuteScalar());
            string temp_sum = Convert.ToString(comm.ExecuteScalar());
            double sum;
            if (temp_sum == string.Empty)
                sum = 0;
            else
                sum = Convert.ToDouble(temp_sum);
            sum = sum + Convert.ToInt32(credit);//加上选的这门课一起的总学分

            comm.CommandText = str_sql4;//执行第四句，注册信息
            string flag_register = Convert.ToString(comm.ExecuteScalar());

            comm.CommandText = str_sql5;//执行第五句，退学信息
            string flag_quit = Convert.ToString(comm.ExecuteScalar());

            comm.CommandText = str_sql6;//执行第六句，休学信息
            string flag_rest = Convert.ToString(comm.ExecuteScalar());
            if ((count1 > 0 || priority_no==0 || category.Equals("公共选修") || category.Equals("专业选修")) && count2 == 0 && sum <= 18 && flag_register!=string.Empty && flag_quit==string.Empty && flag_rest==string.Empty)//选课成功
            {
                str_sql = " insert into Select_course_temp(Sno,Cno) values('" + Sno + "' , '" + Cno + "' ) ";
                comm.CommandText = str_sql;
                comm.ExecuteNonQuery();
                Response.Write("选课成功！！");
            }
            else   //选课失败
            {
                if(count1==0)
                    Response.Write("选课失败，缺少先修课程！");
                if(count2>0)
                    Response.Write("选课失败，课程时间冲突！");
                if(sum>18)
                    Response.Write("选课失败，已选课学分已超过18分！");
                if (flag_register == string.Empty)
                    Response.Write("选课失败，学生尚未注册！");
                if (flag_quit != string.Empty || flag_rest != string.Empty)
                    Response.Write("选课失败，学生已退学或休学！");
            }           

        }

        catch (SqlException ex)
        {
            Response.Write(ex.Message);
        }

        connection.Close();//关闭数据库连接
    }
}